/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package ms.jasim.model.event.provider;


import java.util.Collection;
import java.util.List;

import ms.jasim.framework.IConfiguration;
import ms.jasim.framework.ITemplateProvider;
import ms.jasim.framework.JasimFramework;
import ms.jasim.model.JasimTemplate;
import ms.jasim.model.NamedModelObject;
import ms.jasim.model.event.CustomReaction;
import ms.jasim.model.event.EventFactory;
import ms.jasim.model.event.EventPackage;
import ms.jasim.model.event.EventReaction;
import ms.jasim.model.event.ReactionList;
import ms.jasim.model.impl.ModelObjectImpl;
import ms.jasim.model.provider.JasimEditPlugin;
import ms.jasim.model.provider.NamedModelObjectItemProvider;
import ms.utils.JasimUtils;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.ResourceLocator;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
import org.eclipse.emf.edit.provider.IItemPropertySource;
import org.eclipse.emf.edit.provider.IStructuredItemContentProvider;
import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
import org.eclipse.emf.edit.provider.ViewerNotification;

/**
 * This is the item provider adapter for a {@link ms.jasim.model.event.ReactionList} object.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public class ReactionListItemProvider
	extends NamedModelObjectItemProvider
	implements
		IEditingDomainItemProvider,
		IStructuredItemContentProvider,
		ITreeItemContentProvider,
		IItemLabelProvider,
		IItemPropertySource {
	/**
	 * This constructs an instance from a factory and a notifier.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public ReactionListItemProvider(AdapterFactory adapterFactory) {
		super(adapterFactory);
	}

	/**
	 * This returns the property descriptors for the adapted class.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public List<IItemPropertyDescriptor> getPropertyDescriptors(Object object) {
		if (itemPropertyDescriptors == null) {
			super.getPropertyDescriptors(object);

		}
		return itemPropertyDescriptors;
	}

	/**
	 * This specifies how to implement {@link #getChildren} and is used to deduce an appropriate feature for an
	 * {@link org.eclipse.emf.edit.command.AddCommand}, {@link org.eclipse.emf.edit.command.RemoveCommand} or
	 * {@link org.eclipse.emf.edit.command.MoveCommand} in {@link #createCommand}.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Collection<? extends EStructuralFeature> getChildrenFeatures(Object object) {
		if (childrenFeatures == null) {
			super.getChildrenFeatures(object);
			childrenFeatures.add(EventPackage.Literals.REACTION_LIST__REACTION);
		}
		return childrenFeatures;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EStructuralFeature getChildFeature(Object object, Object child) {
		// Check the type of the specified child object and return the proper feature to use for
		// adding (see {@link AddCommand}) it as a child.

		return super.getChildFeature(object, child);
	}

	/**
	 * This returns ReactionList.gif.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object getImage(Object object) {
		return overlayImage(object, getResourceLocator().getImage("full/obj16/ReactionList"));
	}

	/**
	 * This returns the label text for the adapted class.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 */
	@Override
	public String getText(Object object) {
		String label = ((ReactionList)object).getName();
		return label == null || label.length() == 0 ?
			getString("_UI_ReactionList_type") : label;
	}

	/**
	 * This handles model notifications by calling {@link #updateChildren} to update any cached
	 * children and by creating a viewer notification, which it passes to {@link #fireNotifyChanged}.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void notifyChanged(Notification notification) {
		updateChildren(notification);

		switch (notification.getFeatureID(ReactionList.class)) {
			case EventPackage.REACTION_LIST__REACTION:
				fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false));
				return;
		}
		super.notifyChanged(notification);
	}

	/**
	 * This adds {@link org.eclipse.emf.edit.command.CommandParameter}s describing the children
	 * that can be created under this object.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	@Override
	protected void collectNewChildDescriptors(Collection<Object> newChildDescriptors, Object object) {
		super.collectNewChildDescriptors(newChildDescriptors, object);

		ReactionList list = (ReactionList) object;
		boolean isAdvMode = JasimFramework.getConfiguration().getBoolean(IConfiguration.ADVANCED_MODE, false);
		boolean isCustomActionTemplate = list.eContainer() instanceof JasimTemplate;
		
		if (isCustomActionTemplate)
			newChildDescriptors.add
			(createChildParameter
				(EventPackage.Literals.REACTION_LIST__REACTION,
				 EventFactory.eINSTANCE.createCustomReaction()));
		else if (list.isTemplateObject() || (isAdvMode)) {
			newChildDescriptors.add
				(createChildParameter
					(EventPackage.Literals.REACTION_LIST__REACTION,
					 EventFactory.eINSTANCE.createModifyModelReaction()));
	
			newChildDescriptors.add
				(createChildParameter
					(EventPackage.Literals.REACTION_LIST__REACTION,
					 EventFactory.eINSTANCE.createReplanReaction()));
			
			newChildDescriptors.add
			(createChildParameter
				(EventPackage.Literals.REACTION_LIST__REACTION,
				 EventFactory.eINSTANCE.createForAllReaction()));
			
			newChildDescriptors.add
			(createChildParameter
				(EventPackage.Literals.REACTION_LIST__REACTION,
				 EventFactory.eINSTANCE.createModifyObjectReaction()));
			
			Object rootObj = ((ModelObjectImpl) list.eContainer()).getRootObject();
			
			if (rootObj instanceof JasimTemplate) {
				JasimTemplate template = (JasimTemplate) rootObj;
				if (template.getCustomReactions() != null)
					for (EventReaction act: template.getCustomReactions().getReaction()) {
						EventReaction copy = (EventReaction) EcoreUtil.copy(act);
						JasimUtils.setPredefined(copy);
						copy.setPredefined(false);
						copy.setReservedName(copy.getName() != null && copy.getName().length() > 0);
						newChildDescriptors.add
						(createChildParameter
							(EventPackage.Literals.REACTION_LIST__REACTION, copy));
					}
			}
			else {
				ITemplateProvider provider = JasimFramework.getService(null, ITemplateProvider.class);
				if (provider !=  null) {
					List<EventReaction> actions = provider.getCustomReactionTemplates();
					if (actions != null)
						for (EventReaction act: actions)
							newChildDescriptors.add
							(createChildParameter
								(EventPackage.Literals.REACTION_LIST__REACTION, act));
				}
			}
		}
	}

	/**
	 * Return the resource locator for this item provider's resources.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public ResourceLocator getResourceLocator() {
		return JasimEditPlugin.INSTANCE;
	}

	@Override
	public String getCreateChildText(Object owner, Object feature,
			Object child, Collection<?> selection) {
		if (child instanceof CustomReaction && ((CustomReaction) child).getName() != null)
			return ((NamedModelObject) child).getName();
		return super.getCreateChildText(owner, feature, child, selection);
	}
}
